// 等待DOM加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
  // 侧边栏切换功能
  document.getElementById('toggleSidebar').addEventListener('click', function() {
    const sidebar = document.getElementById('sidebar');
    sidebar.classList.toggle('collapsed');
  });

  // 移动端侧边栏切换
  document.getElementById('mobileToggle').addEventListener('click', function() {
    const sidebar = document.getElementById('sidebar');
    sidebar.classList.toggle('collapsed');
  });

  // 头像上传预览
  document.getElementById('avatarUpload').addEventListener('change', function(e) {
    const file = e.target.files[0];
    if (file) {
      const reader = new FileReader();
      reader.onload = function(event) {
        const preview = document.getElementById('avatarPreview');
        preview.querySelector('img').src = event.target.result;
        preview.style.display = 'block';
      };
      reader.readAsDataURL(file);
    }
  });

  // 多文件上传列表展示
  document.getElementById('productUpload').addEventListener('change', function(e) {
    const files = e.target.files;
    const fileList = document.getElementById('fileList');
    fileList.innerHTML = ''; // 清空现有列表

    if (files.length > 0) {
      const list = document.createElement('ul');
      list.className = 'list-group';
      for (let i = 0; i < files.length; i++) {
        const item = document.createElement('li');
        item.className = 'list-group-item d-flex justify-content-between align-items-center';
        item.innerHTML = `
          <span><i class="fa fa-file-image-o me-2"></i>${files[i].name}</span>
          <span class="badge bg-secondary">${(files[i].size / 1024).toFixed(1)}KB</span>
        `;
        list.appendChild(item);
      }
      fileList.appendChild(list);
    }
  });

  // 上传按钮点击事件
  document.getElementById('submitUpload').addEventListener('click', function() {
    // 模拟上传成功提示
    const toast = new bootstrap.Toast(document.createElement('div'));
    toast._element.className = 'toast text-white bg-success position-fixed bottom-4 right-4';
    toast._element.innerHTML = `
      <div class="d-flex">
        <div class="toast-body">文件上传成功！</div>
        <button type="button" class="btn-close btn-close-white me-2 m-auto" data-bs-dismiss="toast"></button>
      </div>
    `;
    document.body.appendChild(toast._element);
    toast.show();
  });
});
// index.html 的 scripts 块中修改 submitUpload 事件
document.getElementById('submitUpload').addEventListener('click', async function() {
    const avatarFile = document.getElementById('avatarUpload').files[0];
    if (!avatarFile) {
        alert('请选择头像文件');
        return;
    }

    const formData = new FormData();
    formData.append('file', avatarFile);

    try {
        const response = await fetch('/upload/avatar', {
            method: 'POST',
            body: formData
        });
        const res = await response.json();
        if (res.code === 200) {
            // 上传成功后更新预览图（使用后端返回的文件路径）
            const preview = document.getElementById('avatarPreview');
            preview.style.display = 'block';
            preview.querySelector('img').src = res.url;
            alert('上传成功');
        } else {
            alert('上传失败：' + res.msg);
        }
    } catch (error) {
        console.error('上传错误：', error);
        alert('上传失败');
    }
});